package com.lianniu.mgr.baseService.staff.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import com.lianniu.mgr.baseService.area.dao.ISystemAreaMapperDao;
import com.lianniu.mgr.baseService.modifypassword.dao.IModifyPasswordMapperDao;
import com.lianniu.mgr.baseService.staff.model.*;
import org.apache.log4j.Logger;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.area.dao.IAdminAreaMapperDao;
import com.lianniu.mgr.baseService.area.model.AdminAreaVo;
import com.lianniu.mgr.baseService.dict.dao.IDictionaryDao;
import com.lianniu.mgr.baseService.post.dao.IPostMapperDao;
import com.lianniu.mgr.baseService.post.model.Post2OrgVo;
import com.lianniu.mgr.baseService.staff.dao.ILogMapperDao;
import com.lianniu.mgr.baseService.staff.dao.IStaffMapperDao;
import com.lianniu.mgr.baseService.staff.service.IStaffService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.constants.UserErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.baseService.post.model.PostVo;
import com.lianniu.mgr.util.MD5Util;
import com.lianniu.mgr.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;

@Service
public class StaffServiceImpl implements IStaffService {

  @Resource
  private IStaffMapperDao staffDao;

  @Resource
  private IAdminAreaMapperDao adminAreaMapperDao;

  @Resource
  private IPostMapperDao postMapperDao;
  
  @Resource
  private ILogMapperDao logDao;
  @Resource
  IModifyPasswordMapperDao modifyPasswordMapperDao;

  @Resource
  private IDictionaryDao dictionaryDao;


 @Resource
 private ISystemAreaMapperDao systemAreaMapperDao;

  private static String SEX_NAME="性别";
  
  private static String EMPLOY_STATUS_NAME="职位状态";

  private static String QUANGUO_ID="2";

  private final static Logger logger = Logger.getLogger(StaffServiceImpl.class);

  /**
   * @Description:添加员工信息
   * @param staffVo
   * @return BaseResult

   * @Date:2016年3月31日下午2:18:31
   * @Version 1.1.0
   */
  @Override
  public BaseResult addStaff(StaffVo staffVo) {
    logger.info("StaffServiceImpl addStaff begin.");
    logger.info("staffVo:" + JSON.toJSONString(staffVo));
    //参数校验
    logger.info("input data check begin.");
    if (paramCheck(staffVo) != null) {
      return paramCheck(staffVo);
    }
    logger.info("input data check end.");

    staffVo.setStaffId(StringUtil.getUUID());
    staffVo.setLoginPassword(MD5Util.getMD5String("123456"));
    logger.info("addStaff begin.");
    staffDao.addStaff(staffVo);
    logger.info("addStaff end.");
    
    logger.info("StaffServiceImpl addStaff end.");
    return BaseResult.success(staffVo.getStaffId());
  }

  /**
   * @Description:编辑员工信息
   * @param staffVo
   * @return BaseResult

   * @Date:2016年3月31日下午2:18:54
   * @Version 1.1.0
   */
  @Override
  @Transactional
  public BaseResult editStaff(StaffVo staffVo) {
    logger.info("StaffServiceImpl editStaff begin.");
    logger.info("staffVo:" + JSON.toJSONString(staffVo));
    //参数校验
    logger.info("input data check begin.");
    if (paramCheck(staffVo) != null) {
      return paramCheck(staffVo);
    }
    logger.info("input data check end.");
    
    //只传入staffId查询编辑前的员工信息
    StaffVo tmp=new StaffVo();
    tmp.setStaffId(staffVo.getStaffId());
    //获取编辑前的员工信息
    logger.info("getStaff begin.");
    StaffVo lastStaffInfo=(StaffVo) getStaff(tmp).getData();
    logger.info("getStaff end.");
    //编辑员工
    logger.info("editStaff begin.");
    staffDao.editStaff(staffVo);
    logger.info("editStaff end.");
    //获取编辑后的员工信息
    logger.info("getStaff begin.");
    StaffVo nowStaffInfo=(StaffVo) getStaff(staffVo).getData();
    logger.info("getStaff end.");
    
    logger.info(JSON.toJSON(lastStaffInfo));
    logger.info(JSON.toJSON(nowStaffInfo));
    //将字典值转换为字典名，记录Log用
    lastStaffInfo.setSex(getDictNameByDictVal(SEX_NAME,lastStaffInfo.getSex()));
    nowStaffInfo.setSex(getDictNameByDictVal(SEX_NAME,nowStaffInfo.getSex()));
    lastStaffInfo.setEmployStatus(getDictNameByDictVal(EMPLOY_STATUS_NAME,lastStaffInfo.getEmployStatus()));
    nowStaffInfo.setEmployStatus(getDictNameByDictVal(EMPLOY_STATUS_NAME,nowStaffInfo.getEmployStatus()));
    
    String modifyUser=staffVo.getModifyUser();
    
    //获得编辑员工日志信息
    String log=getEditStaffLog(lastStaffInfo, nowStaffInfo, modifyUser);
    
    //如果有员工信息变化则记录log
    if(!StringUtil.isEmpty(log)){
      LogVo logVo=new LogVo();
      String logStr=log.substring(0,log.length()-1);
      logVo.setLogId(StringUtil.getUUID());
      logVo.setStaffId(staffVo.getStaffId());
      logVo.setLog(logStr);
      logVo.setAddUser(modifyUser);
      logger.info("addLog begin.");
      logDao.addLog(logVo);
      logger.info("addLog end.");
    }
      
    logger.info("StaffServiceImpl editStaff end.");
    return BaseResult.success(null);
  }

  @Override
  public BaseResult getStaff(StaffVo staffVo) {
    logger.info("getStaff begin.");
    logger.info("staffVo:" + JSON.toJSONString(staffVo));
    List<StaffVo> list = staffDao.getStaff(staffVo);
    if (list == null || list.size() == 0) {
      return BaseResult.fail(UserErrorCode.UserNotExistErrorRetCode,
              UserErrorCode.UserNotExistErrorRetInfo);
    } else {

      StaffVo retStaffVo = list.get(0);
      String areaText = "";
      if (!StringUtil.isEmpty(retStaffVo.getProvinceId())) {
        AdminAreaVo adminAreaVo = adminAreaMapperDao
                .getAreaInfoByAreaId(retStaffVo.getProvinceId());
        if (adminAreaVo != null) {
          areaText += adminAreaVo.getAreaName();
        }
      }
      if (!StringUtil.isEmpty(retStaffVo.getCityId())) {
        AdminAreaVo adminAreaVo = adminAreaMapperDao
                .getAreaInfoByAreaId(retStaffVo.getCityId());
        if (adminAreaVo != null) {
          areaText += adminAreaVo.getAreaName();
        }
        // areaText +=
        // adminAreaMapperDao.findAreaInfoByAreaId(retStaffVo.getCityId()).getAreaName();
      }
      if (!StringUtil.isEmpty(retStaffVo.getCountyId())) {
        AdminAreaVo adminAreaVo = adminAreaMapperDao
                .getAreaInfoByAreaId(retStaffVo.getCountyId());
        if (adminAreaVo != null) {
          areaText += adminAreaVo.getAreaName();
        }
        // areaText +=
        // adminAreaMapperDao.findAreaInfoByAreaId(retStaffVo.getCountyId()).getAreaName();
      }
      retStaffVo.setAreaText(areaText);

      List<Staff2PostVo> staff2PostVos = staffDao
              .getStaff2Post(retStaffVo);

      if (staff2PostVos == null || staff2PostVos.size() == 0) {
        // 员工没有岗位
        logger.info("getStaff StaffVo:" + JSON.toJSONString(retStaffVo));
        logger.info("getStaff end.");
        return BaseResult.success(retStaffVo);
      }

      // 获取员工的岗位列表
      List<Post2OrgVo> RetpostVos = new ArrayList<Post2OrgVo>();
      for (Staff2PostVo staff2PostVo : staff2PostVos) {
        Post2OrgVo post2OrgVo = new Post2OrgVo();
        post2OrgVo.setId(staff2PostVo.getPostId());
        List<Post2OrgVo> post2OrgVos = postMapperDao
                .getPostList(post2OrgVo);
        if (post2OrgVos != null && post2OrgVos.size() != 0) {
          String orgName = post2OrgVos.get(0).getOrgName()
                  .replaceAll(" ", ">");
          post2OrgVos.get(0).setOrgName(orgName);
          RetpostVos.add(post2OrgVos.get(0));
        }
      }
      if (RetpostVos == null || RetpostVos.size() == 0) {
        // 员工没有岗位
        logger.info("getStaff StaffVo:" + JSON.toJSONString(retStaffVo));
        logger.info("getStaff end.");
        return BaseResult.success(retStaffVo);
      }

      retStaffVo.setPost2OrgVos(RetpostVos);

      logger.info("getStaff StaffVo:" + JSON.toJSONString(retStaffVo));
      logger.info("getStaff end.");
      return BaseResult.success(retStaffVo);
    }
  }

  /**
   * @Description：分页查询员工列表
   * @Param:StaffVo，PaginationInfo
   * @Return:BaseResult

   * @Date:2016-4-1 15:10:41
   */
  @Override
  public BaseResult getStaffList(StaffVo staffVo, PaginationInfo paginationInfo) {
    logger.info("getStaffList begin");
    if (!StringUtil.isEmpty(staffVo.getStaffName())) {
      staffVo.setStaffName(staffVo.getStaffName().replace(" ", ""));
    }
    logger.info("staff_name: " + staffVo.getStaffName());

    staffVo.setPaginationInfo(paginationInfo);
    List<StaffVo> list = staffDao.getStaffListForPage(staffVo);
    Map<String, Object> retmap = new HashMap<String, Object>();
    retmap.put("total", paginationInfo.getTotalRecord());
    retmap.put("curPage", paginationInfo.getCurPage());
    retmap.put("rows", list);

    return BaseResult.success(retmap);
  };

  /**
   * @Description：删除员工
   * @Param:modifyUser 修改人 id 删除员工id
   * @Return:删除行数

   * @Date:2016-4-1 15:44:08
   */
  @Transactional
  @Override
  public BaseResult delStaff(String modifyUser, String... id) {
    logger.info("delStaff begin");
    logger.info("delStaff: input data check begin");
    logger.info("modifyUser: " + modifyUser);
    logger.info("id: " + id.toString());
    BaseResult baseResult;
    if (id == null || id.length <= 0) {
      baseResult =
          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("delStaff: input data check end");

    return BaseResult.success(staffDao.delStaff(modifyUser, id));
  }

  /**
   * @Description:获得组织岗位信息
   * @return BaseResult

   * @Date:2016年4月6日下午12:47:15
   * @Version 1.1.0
   */
  @Override
  public BaseResult getOrgPost() {
    logger.info("StaffServiceImpl getOrgPost begin.");
    
    Post2OrgVo post2OrgVo=new Post2OrgVo();
	logger.info("getOrgPost begin.");
	List<Post2OrgVo> list = postMapperDao.getPostListForPage(post2OrgVo);
    logger.info("getOrgPost end.");
    
    logger.info("StaffServiceImpl getOrgPost end.");
    return BaseResult.success(list);
  }

  /**
   * @Description:保存组织岗位信息
   * @return BaseResult
   * @Date:2016年4月6日下午12:47:58
   * @Version 1.1.0
   */
  @Override
  @Transactional(value = "mtransactionManager")
  public BaseResult addPost(ParamVo paramVo) {
    logger.info("StaffServiceImpl addPost begin.");

    //执行insert staff2post
    if(paramVo.getAdds()!=null&&paramVo.getAdds().length>0){
      List<Staff2PostVo> li=new ArrayList<Staff2PostVo>();
      for(String add:paramVo.getAdds()){
        Staff2PostVo vo=new Staff2PostVo();
        vo.setId(StringUtil.getUUID());
        vo.setStaffId(paramVo.getStaffId());
        vo.setPostId(add);
        li.add(vo);
      }

      logger.info("StaffServiceImpl addPost insertStaff2Post begin.");
      staffDao.insertStaff2Post(li);
      logger.info("StaffServiceImpl addPost insertStaff2Post end.");
    }

    //执行delete staff2post
    if(paramVo.getDels()!=null&&paramVo.getDels().length>0){
      logger.info("StaffServiceImpl addPost delStaff2Post begin.");
      staffDao.delStaff2PostByParamVo(paramVo);
      logger.info("StaffServiceImpl addPost delStaff2Post end.");
    }

    //记录分配岗位log
    if(!StringUtil.isEmpty(paramVo.getLog())){
      LogVo logVo=new LogVo();
      logVo.setLogId(StringUtil.getUUID());
      logVo.setStaffId(paramVo.getStaffId());
      logVo.setLog(paramVo.getLog());
      logVo.setAddUser(paramVo.getAddUser());
      logger.info("StaffServiceImpl addPost addLog begin.");
      logDao.addLog(logVo);
      logger.info("StaffServiceImpl addPost addLog end.");
    }

    logger.info("StaffServiceImpl addPost end.");
    return BaseResult.success();
  }

  /**
   * @Description:根据员工Id获得已被勾选的岗位
   * @param staffId
   * @return BaseResult

   * @Date:2016年4月20日下午2:49:17
   * @Version 1.1.0
   */
  @Override
  public BaseResult getCheckedPostByStaffId(String staffId) {
    logger.info("StaffServiceImpl getCheckedPostByStaffId begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(staffId)) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("input data check end.");
    
    logger.info("getCheckedPostByStaffId begin.");
    List<String> list=staffDao.getCheckedPostByStaffId(staffId);
    logger.info("getCheckedPostByStaffId end.");
    
    logger.info("StaffServiceImpl getCheckedPostByStaffId end.");
    return BaseResult.success(list);
  }

  /**
   * @Description:检证员工编号是否存在
   * @param request
   * @return true:不存在 false:存在

   * @Date:2016年4月7日下午12:56:34
   * @Version 1.0.0
   */
  @Override
  public boolean isExistStaffNo(String staffNo) {
    logger.info("StaffServiceImpl isExistStaffNo begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(staffNo)) {
      return false;
    }
    logger.info("input data check end.");
    
    logger.info("isExistStaffNo begin.");
    boolean isExistFlg=staffDao.isExistStaffNo(staffNo);
    boolean retFlg = isExistFlg?false:true;
    logger.info("isExistStaffNo end.");
    
    logger.info("StaffServiceImpl isExistStaffNo end.");
    return retFlg;
  }

  /**
   * @Description:检证员工用户名是否存在
   * @param request
   * @return true:不存在 false:存在

   * @Date:2016年4月7日下午12:56:34
   * @Version 1.0.0
   */
  @Override
  public boolean isExistLoginName(String loginName) {
    logger.info("StaffServiceImpl isExistLoginName begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(loginName)) {
      return false;
    }
    logger.info("input data check end.");
    
    logger.info("isExistLoginName begin.");
    boolean isExistFlg=staffDao.isExistLoginName(loginName);
    boolean retFlg = isExistFlg?false:true;
    logger.info("isExistLoginName end.");
    
    logger.info("StaffServiceImpl isExistLoginName end.");
    return retFlg;
  }

  /**
   * @Description:检证员工手机号是否存在
   * @param request
   * @return true:不存在 false:存在

   * @Date:2016年4月7日下午12:56:34
   * @Version 1.0.0
   */
  @Override
  public boolean isExistMobile(String mobile) {
    logger.info("StaffServiceImpl isExistMobile begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(mobile)) {
      return false;
    }
    logger.info("input data check end.");
    
    logger.info("isExistMobile begin.");
    boolean isExistFlg=staffDao.isExistMobile(mobile);
    boolean retFlg = isExistFlg?false:true;
    logger.info("isExistMobile end.");
    
    logger.info("StaffServiceImpl isExistMobile end.");
    return retFlg;
  }

  /**
   * @Description:检证员工姓名是否存在
   * @param request
   * @return true:不存在 false:存在

   * @Date:2016年4月7日下午12:56:34
   * @Version 1.0.0
   */
  @Override
  public boolean isExistStaffName(String staffName) {
    logger.info("StaffServiceImpl isExistStaffName begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(staffName)) {
      return false;
    }
    logger.info("input data check end.");
    
    logger.info("isExistStaffName begin.");
    boolean isExistFlg=staffDao.isExistStaffName(staffName);
    boolean retFlg = isExistFlg?false:true;
    logger.info("isExistStaffName end.");
    
    logger.info("StaffServiceImpl isExistStaffName end.");
    return retFlg;
  }

  /**
   * @Description:检证员工邮箱是否存在
   * @param request
   * @return true:不存在 false:存在

   * @Date:2016年4月7日下午12:56:34
   * @Version 1.0.0
   */
  @Override
  public boolean isExistEmail(String email) {
    logger.info("StaffServiceImpl isExistEmail begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(email)) {
      return false;
    }
    logger.info("input data check end.");
    
    logger.info("isExistEmail begin.");
    boolean isExistFlg=staffDao.isExistEmail(email);
    boolean retFlg = isExistFlg?false:true;
    logger.info("isExistEmail end.");
    
    logger.info("StaffServiceImpl isExistEmail end.");
    return retFlg;

  }
  
  /**
   * @Description:检证员工编号是否存在(编辑)
   * @param request
   * @return true:不存在 false:存在

   * @Date:2016-4-7 21:16:24
   * @Version 1.0.0
   */
  public boolean isExistStaffNoById(String staffNo,String staffId){
    logger.info("StaffServiceImpl isExistStaffNoById begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(staffNo)||StringUtil.isEmpty(staffId)) {
      return false;
    }
    logger.info("input data check end.");
    
    logger.info("isExistStaffNoById begin.");
    boolean isExistFlg=staffDao.isExistStaffNoById(staffNo,staffId);
    boolean retFlg = isExistFlg?false:true;
    logger.info("isExistStaffNoById end.");
    
    logger.info("StaffServiceImpl isExistStaffNoById end.");
    return retFlg;
  }
  
  /**
   * @Description:检证员工用户名是否存在(编辑)
   * @param request
   * @return true:不存在 false:存在

   * @Date:2016-4-7 21:16:24
   * @Version 1.0.0
   */
  public boolean isExistLoginNameById(String loginName,String staffId){
    logger.info("StaffServiceImpl isExistLoginNameById begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(loginName)||StringUtil.isEmpty(staffId)) {
      return false;
    }
    logger.info("input data check end.");
    
    logger.info("isExistLoginNameById begin.");
    boolean isExistFlg=staffDao.isExistLoginNameById(loginName,staffId);
    boolean retFlg = isExistFlg?false:true;
    logger.info("isExistLoginNameById end.");
    
    logger.info("StaffServiceImpl isExistLoginNameById end.");
    return retFlg;
  }
  
  /**
   * @Description:检证员工手机号是否存在(编辑)
   * @param request
   * @return true:不存在 false:存在

   * @Date:2016-4-7 21:16:24
   * @Version 1.0.0
   */
  public boolean isExistMobileById(String mobile,String staffId){
    logger.info("StaffServiceImpl isExistMobileById begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(mobile)||StringUtil.isEmpty(staffId)) {
      return false;
    }
    logger.info("input data check end.");
    
    logger.info("isExistMobileById begin.");
    boolean isExistFlg=staffDao.isExistMobileById(mobile,staffId);
    boolean retFlg = isExistFlg?false:true;
    logger.info("isExistMobileById end.");
    
    logger.info("StaffServiceImpl isExistMobileById end.");
    return retFlg;
  }
  
  /**
   * @Description:检证员工姓名是否存在(编辑)
   * @param request
   * @return true:不存在 false:存在

   * @Date:2016-4-7 21:16:24
   * @Version 1.0.0
   */
  public boolean isExistStaffNameById(String staffName,String staffId){
    logger.info("StaffServiceImpl isExistStaffNameById begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(staffName)||StringUtil.isEmpty(staffId)) {
      return false;
    }
    logger.info("input data check end.");
    
    logger.info("isExistStaffNameById begin.");
    boolean isExistFlg=staffDao.isExistStaffNameById(staffName,staffId);
    boolean retFlg = isExistFlg?false:true;
    logger.info("isExistStaffNameById end.");
    
    logger.info("StaffServiceImpl isExistStaffNameById end.");
    return retFlg;
  }
  
  /**
   * @Description:检证员工邮箱是否存在(编辑)
   * @param request
   * @return true:不存在 false:存在

   * @Date:2016-4-7 21:16:24
   * @Version 1.0.0
   */
  public boolean isExistEmailById(String email,String staffId){
    logger.info("StaffServiceImpl isExistEmailById begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(email)||StringUtil.isEmpty(staffId)) {
      return false;
    }
    logger.info("input data check end.");
    
    logger.info("isExistEmailById begin.");
    boolean isExistFlg=staffDao.isExistEmailById(email,staffId);
    boolean retFlg = isExistFlg?false:true;
    logger.info("isExistEmailById end.");
    
    logger.info("StaffServiceImpl isExistEmailById end.");
    return retFlg;
  }
  
  /**
   * @Description:前台参数校验
   * @param staffVo
   * @return BaseResult

   * @Date:2016年3月31日下午2:17:57
   * @Version 1.0.0
   */
  private BaseResult paramCheck(StaffVo staffVo) {
    //非空校验
    if (StringUtil.isEmpty(staffVo.getStaffNo())) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if (StringUtil.isEmpty(staffVo.getLoginName())) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if (StringUtil.isEmpty(staffVo.getStaffName())) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if (StringUtil.isEmpty(staffVo.getSex())) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if (StringUtil.isEmpty(staffVo.getProvinceId())) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if (StringUtil.isEmpty(staffVo.getCityId())) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if (StringUtil.isEmpty(staffVo.getCountyId())) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if (StringUtil.isEmpty(staffVo.getMobile())) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if (StringUtil.isEmpty(staffVo.getEmail())) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    //格式校验
    System.out.println(staffVo.getStaffNo().length());
    if(staffVo.getStaffNo().length()>12){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if(staffVo.getLoginName().length()>30){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if(staffVo.getStaffName().length()>30){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if(staffVo.getEmail().length()>30){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if(staffVo.getWeixin().length()>30){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if(staffVo.getQq().length()>30){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if(staffVo.getTelePhone().length()>30){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    String mobileRegex="^1[3578]\\d{9}$";
    if(!Pattern.matches(mobileRegex, staffVo.getMobile())){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    String emailRegex="^[a-z\\d]+(\\.[a-z\\d]+)*@([\\da-z](-[\\da-z])?)+(\\.{1,2}[a-z]+)+$";
    if(!Pattern.matches(emailRegex, staffVo.getEmail())){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if(!StringUtil.isEmpty(staffVo.getAreaText())&&staffVo.getAreaText().length()>100){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    return null;
  }
  
  /**
   * @Description:日期格式化
   * @param date
   * @return String

   * @Date:2016年4月11日下午7:15:21
   * @Version 1.0.0
   */
  private String dateFormat(Date date){
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(date);
  }
  
  /**
   * @Description:根据字典类型和字典值查询字典名
   * @param dictTypeName
   * @param dictVal
   * @return String

   * @Date:2016年4月11日下午5:09:02
   * @Version 1.0.0
   */
  private String getDictNameByDictVal(String dictTypeName,String dictVal){
    logger.info("getDictNameByDictVal begin.");
    String dictName=dictionaryDao.getDictNameByDictVal(dictTypeName, dictVal);
    logger.info("getDictNameByDictVal end.");
    return dictName;
  }
  
  /**
   * @Description:获得编辑员工日志信息
   * @param lastStaffInfo
   * @param nowStaffInfo
   * @param modifyUser
   * @return String

   * @Date:2016年4月20日下午2:41:08
   * @Version 1.0.0
   */
  private String getEditStaffLog(StaffVo lastStaffInfo,StaffVo nowStaffInfo,String modifyUser){
    StringBuffer sb=new StringBuffer();
    
    //日志记录
    if(!compare(lastStaffInfo.getStaffNo(),nowStaffInfo.getStaffNo()))
      sb.append("【员工编号】由  "+modifyUser+" 从  \""+lastStaffInfo.getStaffNo()+"\"修改为  \""+nowStaffInfo.getStaffNo()+"\",");
    if(!compare(lastStaffInfo.getLoginName(),nowStaffInfo.getLoginName()))
      sb.append("【用户名】由  "+modifyUser+" 从  \""+lastStaffInfo.getLoginName()+"\"修改为  \""+nowStaffInfo.getLoginName()+"\",");
    if(!compare(lastStaffInfo.getStaffName(),nowStaffInfo.getStaffName()))
      sb.append("【姓名】由  "+modifyUser+" 从  \""+lastStaffInfo.getStaffName()+"\"修改为  \""+nowStaffInfo.getStaffName()+"\",");
    if(!compare(lastStaffInfo.getSex(),nowStaffInfo.getSex()))
      sb.append("【性别】由  "+modifyUser+" 从  \""+lastStaffInfo.getSex()+"\"修改为  \""+nowStaffInfo.getSex()+"\",");
    if(!compare(lastStaffInfo.getBirthday(),nowStaffInfo.getBirthday()))
      sb.append("【出生日期】由  "+modifyUser+" 从  \""+dateFormat(lastStaffInfo.getBirthday())+"\"修改为  \""+dateFormat(nowStaffInfo.getBirthday())+"\",");
    if(!compare(lastStaffInfo.getAreaText(),nowStaffInfo.getAreaText()))
      sb.append("【所在区域】由  "+modifyUser+" 从  \""+lastStaffInfo.getAreaText()+"\"修改为  \""+nowStaffInfo.getAreaText()+"\",");
    if(!compare(lastStaffInfo.getEmployTime(),nowStaffInfo.getEmployTime()))
      sb.append("【入职时间】由  "+modifyUser+" 从  \""+dateFormat(lastStaffInfo.getEmployTime())+"\"修改为  \""+dateFormat(nowStaffInfo.getEmployTime())+"\",");
    if(!compare(lastStaffInfo.getEmployStatus(),nowStaffInfo.getEmployStatus()))
      sb.append("【职位状态】由  "+modifyUser+" 从  \""+lastStaffInfo.getEmployStatus()+"\"修改为  \""+nowStaffInfo.getEmployStatus()+"\",");
    if(!compare(lastStaffInfo.getMobile(),nowStaffInfo.getMobile()))
      sb.append("【手机】由  "+modifyUser+" 从  \""+lastStaffInfo.getMobile()+"\"修改为  \""+nowStaffInfo.getMobile()+"\",");
    if(!compare(lastStaffInfo.getEmail(),nowStaffInfo.getEmail()))
      sb.append("【邮箱】由  "+modifyUser+" 从  \""+lastStaffInfo.getEmail()+"\"修改为  \""+nowStaffInfo.getEmail()+"\",");
    if(!compare(lastStaffInfo.getWeixin(),nowStaffInfo.getWeixin()))
      sb.append("【微信】由  "+modifyUser+" 从  \""+lastStaffInfo.getWeixin()+"\"修改为  \""+nowStaffInfo.getWeixin()+"\",");
    if(!compare(lastStaffInfo.getQq(),nowStaffInfo.getQq()))
      sb.append("【QQ】由  "+modifyUser+" 从  \""+lastStaffInfo.getQq()+"\"修改为  \""+nowStaffInfo.getQq()+"\",");
    if(!compare(lastStaffInfo.getTelePhone(),nowStaffInfo.getTelePhone()))
      sb.append("【办公室电话】由  "+modifyUser+" 从  \""+lastStaffInfo.getTelePhone()+"\"修改为  \""+nowStaffInfo.getTelePhone()+"\",");
    if(!compare(lastStaffInfo.getStaffDesc(),nowStaffInfo.getStaffDesc()))
      sb.append("【个人说明】由  "+modifyUser+" 从  \""+lastStaffInfo.getStaffDesc()+"\"修改为  \""+nowStaffInfo.getStaffDesc()+"\",");
    
    return sb.toString();
  }
  
  /**
   * @Description:null转""已应对字符串比较equals方法
   * @param obj1
   * @param obj2
   * @return boolean

   * @Date:2016年4月25日下午2:03:36
   * @Version 1.0.0
   */
  private boolean compare(Object obj1,Object obj2){
    String str1="";
    String str2="";
    if(obj1 instanceof Date){
      str1=dateFormat((Date)obj1);
    }
    if(obj2 instanceof Date){
      str2=dateFormat((Date)obj2);
    }
    if(obj1 instanceof String){
      str1=(String) obj1;
    }
    if(obj2 instanceof String){
      str2=(String) obj2;
    }
    if(null==str1){
      str1="";
    }
    if(null==str2){
      str2="";
    }
    return str1.equals(str2);
  }

  /**
   * @Description:页面删除岗位，员工是否还存在岗位(1有，0没有)
   * @param staffId
   * @return BaseResult
   * @Date:2016年11月23日上午10:15:30
   * @Version 1.0.0
   */
  @Override
  public BaseResult isPostExistByStaffId(String staffId,String[] dels) {
    logger.info("StaffServiceImpl isPostExistByStaffId begin.");
    logger.info("员工id:"+staffId);

    logger.info("StaffServiceImpl isPostExistByStaffId getPostByStaffId begin.");
    List<PostVo> li=postMapperDao.getPostByStaffId(staffId);
    logger.info("StaffServiceImpl isPostExistByStaffId getPostByStaffId end.");

    String flag="0";

    List<String> postIdLi=new ArrayList<String>();
    for(PostVo post:li){
      postIdLi.add(post.getId());
    }

    List<String> delLi=new ArrayList<String>();
    if(dels!=null){
      delLi= Arrays.asList(dels);
    }

    if(postIdLi.size()>delLi.size()&&postIdLi.containsAll(delLi)){
      flag="1";
    }
    logger.info("StaffServiceImpl isPostExistByStaffId end.");
    return BaseResult.success(flag);
  }
  /**
   * @Description:员工分配岗位
   * @param staffId
   * @param paginationInfo
   * @return BaseResult
   * @Date:2016年10月20日上午10:30:38
   * @Version 1.1.0
   */
  @Override
  public BaseResult assignPost(Post2OrgVo post2OrgVo,PaginationInfo paginationInfo) {
    logger.info("StaffServiceImpl assignPost begin.");
    logger.info("员工组织岗位信息&staffId:"+JSON.toJSONString(post2OrgVo));
    logger.info("分页信息&paginationInfo:"+JSON.toJSONString(paginationInfo));

    post2OrgVo.setPaginationInfo(paginationInfo);
    logger.info("StaffServiceImpl assignPost getPostListByStaffIdForPage begin.");
    List<Post2OrgVo> list = postMapperDao.getPostListByStaffIdForPage(post2OrgVo);
    logger.info("StaffServiceImpl assignPost getPostListByStaffIdForPage 岗位组织信息&list:"+JSON.toJSONString(list));
    logger.info("StaffServiceImpl assignPost getPostListByStaffIdForPage end.");

    Map<String, Object> retmap = new HashMap<String, Object>();
    retmap.put("total", paginationInfo.getTotalRecord());
    retmap.put("curPage", paginationInfo.getCurPage());
    retmap.put("rows", list);

    return BaseResult.success(retmap);
  }

  /**
   * @Description:重置密码，默认设置为1q2w3e
   * @param staffId
   * @Date:2016年12月12日
   */
  @Override
  public BaseResult resetPwdByStaffId(String staffId,String username) {
    logger.info("StaffServiceImpl resetPwdByStaffId begin.");
    logger.info("StaffServiceImpl resetPwdByStaffId staffId&员工id:" + staffId);
    logger.info("StaffServiceImpl resetPwdByStaffId username&修改者:" + username);

    //得到员工信息
    logger.info("StaffServiceImpl resetPwdByStaffId getStaffInfoByStaffId begin.");
    StaffVo staffVo = modifyPasswordMapperDao.getStaffInfoByStaffId(staffId);
    logger.info("StaffServiceImpl resetPwdByStaffId getStaffInfoByStaffId staffVo&登录员工信息："
            + JSON.toJSONString(staffVo));
    logger.info("StaffServiceImpl resetPwdByStaffId getStaffInfoByStaffId end.");

    //重置密码
    logger.info("StaffServiceImpl resetPwdByStaffId resetPwdByStaffVo begin.");
    staffVo.setLoginPassword(MD5Util.getMD5String("1q2w3e"));
    int resetFlag = staffDao.resetPwdByStaffVo(staffVo);
    logger.info("StaffServiceImpl resetPwdByStaffId resetPwdByStaffVo resetFlag&重置密码标识："
            + JSON.toJSONString(resetFlag));
    logger.info("StaffServiceImpl resetPwdByStaffId resetPwdByStaffVo end.");

    //重置密码操作日志记录
    LogVo logVo = new LogVo();
    logVo.setLogId(StringUtil.getUUID());
    logVo.setStaffId(staffVo.getStaffId());
    logVo.setAddUser(username);
    String logInfo = "由管理员将"+staffVo.getLoginName()+"的密码重置为初始密码";
    logVo.setLog(logInfo);
    logger.info("StaffServiceImpl resetPwdByStaffId addLog begin.");
    logDao.addLog(logVo);
    logger.info("StaffServiceImpl resetPwdByStaffId addLog end.");

    logger.info("StaffServiceImpl resetPwdByStaffId end.");
    return BaseResult.success(resetFlag);
  }
    /**
     * @Description:员工详情页查看数据权限
     * @param staffId
     * @return BaseResult
     * @Date:2016年10月14日下午2:14:13
     * @Version 1.1.0
     */
    @Override
    public BaseResult checkPrecintByStaffId(String staffId) {
        logger.info("StaffServiceImpl checkPrecintByStaffId begin.");
        logger.info("staffId&员工id:"+staffId);

        logger.info("StaffServiceImpl checkPrecintByStaffId getStaff2CountyByStaffId begin.");
        List<String> countyLi=systemAreaMapperDao.getStaff2CountyByStaffId(staffId);
        logger.info("StaffServiceImpl checkPrecintByStaffId getStaff2CountyByStaffId countyLi&县id:"+JSON.toJSONString(countyLi));
        logger.info("StaffServiceImpl checkPrecintByStaffId getStaff2CountyByStaffId end.");

        //用户没有数据权限
        if(countyLi.size()<1){
            return BaseResult.success();
        }

        logger.info("StaffServiceImpl checkPrecintByStaffId getParentIdsByAreaIds begin.");
        List<String> cityLi=systemAreaMapperDao.getParentIdsByAreaIds(countyLi);
        logger.info("StaffServiceImpl checkPrecintByStaffId getParentIdsByAreaIds cityLi&市id:"+JSON.toJSONString(cityLi));
        logger.info("StaffServiceImpl checkPrecintByStaffId getParentIdsByAreaIds end.");

        logger.info("StaffServiceImpl checkPrecintByStaffId getParentIdsByAreaIds begin.");
        List<String> provinceLi=systemAreaMapperDao.getParentIdsByAreaIds(cityLi);
        logger.info("StaffServiceImpl checkPrecintByStaffId getParentIdsByAreaIds provinceLi&省id:"+JSON.toJSONString(provinceLi));
        logger.info("StaffServiceImpl checkPrecintByStaffId getParentIdsByAreaIds end.");

        provinceLi.addAll(cityLi);
        provinceLi.addAll(countyLi);
        provinceLi.add(QUANGUO_ID);


        logger.info("StaffServiceImpl checkPrecintByStaffId getAreasByAreaIds begin.");
        List<Staff2SysAreaVo> li=systemAreaMapperDao.getAreasByAreaIds(provinceLi);
        logger.info("StaffServiceImpl checkPrecintByStaffId getAreasByAreaIds li&区域信息："+JSON.toJSONString(li));
        logger.info("StaffServiceImpl checkPrecintByStaffId getAreasByAreaIds end.");

        return BaseResult.success(li);
    }

  @Override
  public BaseResult getStaffByPostion(String postion) {
    logger.info("StaffServiceImpl getStaffByPostion begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(postion)) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("input data check end.");

    logger.info("getStaffByPostion begin.");
    List<StaffVo> list=staffDao.getStaffByPostion(postion);
    logger.info("getStaffByPostion end.");

    logger.info("StaffServiceImpl getStaffByPostion end.");
    return BaseResult.success(list);
  }

  @Override
  public BaseResult selectStaffByPostName(String postName) {
    // TODO Auto-generated method stub
    logger.info("selectStaffByPostName begin");
    logger.info("selectStaffByPostName parm: null");

    return BaseResult.success(staffDao.selectStaffByPostName(postName));
  }


}
